package cn.flying.cloud.task.core.callback.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lmax.disruptor.EventHandler;

import cn.flying.cloud.task.core.callback.JobExecuteCallback;
import cn.flying.cloud.task.core.callback.event.TransferEvent;

/**
 * @author: admin
 * @date: 2025年03月18日 11:05
 * @version: 1.0
 */
public class TransferEventHandler implements EventHandler<TransferEvent> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    JobExecuteCallback jobExecuteCallback;

    public TransferEventHandler(JobExecuteCallback jobExecuteCallback) {
        this.jobExecuteCallback = jobExecuteCallback;
    }

    /**
     * @param event
     * @param sequence
     * @param endOfBatch
     * @throws Exception
     */
    @Override
    public void onEvent(TransferEvent event, long sequence, boolean endOfBatch) throws Exception {
        logger.info("持久化任务执行日志：【{}】", event.getData());
        try {
            if ("job".equals(event.getType())) {
                jobExecuteCallback.log(event.getData());
            } else {
                jobExecuteCallback.step(event.getData());
            }
        } catch (Exception e) {
            logger.error("回调保存任务执行记录异常！{}", e.getMessage());
        }
    }
}
